<?php

header("Content-type: application/xhtml+xml");

require_once('../lib/Smarty/Smarty.class.php');
require_once('../lib/HimaShare.php');
require_once('../lib/Schedule.php');
require_once('../lib/Util.php');
require_once('../lib/lib.mobile.php');
require_once('../lib/Config.php');
require_once('../lib/Note.php');
require_once('../lib/Note/List.php');
require_once('../lib/emoji/Emoji.php');

define('ROWS_PER_PAGE', 10);

$data   = make_page(array_merge($_GET, $_POST));
$smarty = new Smarty();
$smarty->assign($data);
$smarty->display($data['error_code'] ? 'error.tmpl' : 'list_note.tmpl');

function make_page ($params) {
    $data = array('error_code' => 0);

    if (!validate_signature($params)) {
        $data['error_code'] = 1;
        return $data;
    }

    $user   = $params['opensocial_owner_id'];
    $app_id = $params['opensocial_app_id'];


    if (isset($params['date']) && is_date($params['date'])) {
        $start = date_to_unix($params['date']);
    }

    // add note
    if (isset($params['add_note']) && $params['note_title']) {
        $note = new Note();
        $note->member_id = $user;
        $emoji = HTML_Emoji::getInstance();
        //$text = $emoji->filter($params['note_title'], 'input');
        //$note->title     = mb_convert_encoding($text, 'utf-8', 'shift-jis');
        $note->title       = $emoji->filter($params['note_title'], 'input');
        try {
            $note->save();
        } catch (Exception $e) {
            $data['error_code'] = 2;
            echo 'error';
        }
    }

    //change status
    if(isset($params['change_status']) && isset($params['note_id'])){
        $note = new Note($params['note_id']);

        if($note->is_done()){
            $note->set_undone();
        }else{
            $note->set_done();
        }
    }

    //delete note
    if(isset($params['delete_flg']) && isset($params['note_id'])){
        $note = new Note($params['note_id']);
        
        $note->delete();
    }

    //clear checked
    if(isset($params['clear_checked']))
    {
        $data['clear_checked'] = 1;
    }

	//change share flg
	if(isset($params['change_share_flg']) && isset($params['note_id'])){
		$note = new Note($params['note_id']);
		
		if($note->is_public()){
			$note->set_private();
		}else{
			$note->set_public();
		}
		
	}
	
	// get note list
    $my_note_list = Note_List::get_by_member($user);

    //get own profile
    $own_profile = HimaShare::get_member($user);
    $own_profile['displayName'] =  mb_convert_encoding($own_profile['displayName'], 'shift-jis', 'utf8');

	//get friends's note
	$friends = HimaShare::get_all_friends($user);
	//$my_data        = HimaShare::get_member($user);
    //$friends[$user] = $my_data;
	if(count($friends) > 0){
		foreach($friends as $friend)
		{
			$tmp_friend_note = Note_List::get_by_member($friend['id']);
			if(!is_null($tmp_friend_note)){
				foreach ($tmp_friend_note as $key => $entry) {
					$tmp_TODO = new Note();
					$tmp_TODO->load($entry->id);
					if($tmp_TODO->is_public()){
						$friends_note_list[$friend['id']][] = $entry;
					}else{
						unset($friends_note_list[$friend['id']][$key]);
					}
				}
			}
		}
	}
	//$friends_note_list[$user] = $my_note_list;

	//preDump($friends_note_list);

    $data['my_note_list'] = $my_note_list;
    $data['owner_id']  = $user;
	$data['url_prefix'] = URL_PREFIX;
	$data['friends'] = $friends;
	$data['friends_note_list'] = $friends_note_list;
	$data['current_page'] = 'note';
    $data['analytics']    = '<img src="' . googleAnalyticsGetImageUrl() . '" />'; // for google analytics

    return $data;
}

?>
